package com.itheima.web;

import com.itheima.utils.FileUtils;
import io.swagger.annotations.Api;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * company: www.abc.com
 * Author: 29746
 * Create Data: 2020/11/15
 */
@RestController
@RequestMapping("/upload")
@Api("上传管理")
public class UploadController {


    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    //文件上传
    @PostMapping("/uploadFile")
    public String uploadFile(@RequestParam("myfile") MultipartFile mulitpartFile)throws Exception{
        String path=FileUtils.upload(mulitpartFile.getOriginalFilename(),mulitpartFile.getInputStream());
        redisTemplate.opsForSet().add("file:allset",path);
        //对file:{path}设置有效时间
        redisTemplate.opsForValue().set("file:"+path,1,30L, TimeUnit.SECONDS);
        return path;
    }


}
